package com.example.demo.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.domain.Result;
import com.example.demo.entity.Role;
import com.example.demo.entity.User;

/**
 * user控制器
 * @author Yuri
 *
 */
@RestController
//允许跨域请求
@CrossOrigin
public class UserController {

    @GetMapping("/user")
    public Result<User> getOrder() {
    	Result<User> result = new Result<>();
    	try {
    		SecurityContext ctx = SecurityContextHolder.getContext();
    	    Authentication auth = ctx.getAuthentication();
    	    User user = (User) auth.getPrincipal();
    	    result.setData(user);
    	    result.setSuccess("查询成功");
    	    for(Role role : user.getRole()) {
    	    	if("管理员".equalsIgnoreCase(role.getRoleName())) {
    	    		result.setCode(2);
    	    	}
    	    }
    	    result.setCode(2);
		} catch (Exception e) {
			e.printStackTrace();
			result.setError("发生意料之外的错误！");
		}
        return result;
    }


}
